<template>
  <div class="published-container">
    <!-- 操作区域 -->
    <div class="action-bar">
      <el-button 
        type="success" 
        icon="el-icon-plus"
        @click="publishClick"
        class="publish-btn">
        发布{{ cdesciibe }}
      </el-button>
      
      <div class="search-box">
        <el-input
          v-model="searchValue"
          placeholder="请输入商品内容进行搜索"
          clearable
          class="search-input"
          @keyup.enter.native="handleSearch">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="handleSearch">
          </el-button>
        </el-input>
      </div>
    </div>

    <!-- 商品列表 -->
    <div class="goods-list">
      <div 
        v-for="(item, index) in userGoods" 
        :key="index"
        class="goods-card">
        
        <div class="goods-image">
          <el-image
            :src="item.picture ? $store.state.imgShowRoad + '/file/' + item.picture : require('../assets/img/wutu.gif')"
            fit="cover"
            class="goods-img">
            <div slot="error" class="image-error">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </div>
        
        <div class="goods-info">
          <h4 class="goods-title">{{ item.title }}</h4>
          
          <div class="goods-meta">
            <span class="goods-owner">
              <i class="el-icon-user"></i>
              {{ item.ownName }}
            </span>
            
            <p class="goods-content">{{ item.content }}</p>
            
            <div class="goods-footer">
              <span class="goods-price">￥{{ item.price }}</span>
              
              <div class="goods-actions">
                <el-button
                  type="text"
                  icon="el-icon-edit"
                  @click.stop="changeInfo(item.orderId)">
                  编辑
                </el-button>
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  @click.stop="deleteInfo(item.orderId)"
                  style="color: #F56C6C">
                  删除
                </el-button>
                <el-button
                  type="text"
                  v-if="$store.getters.isAdmin && item.type !== 'needs'"
                  @click.stop="item.orderStatus === 0 ? takeDown(item.orderId) : takeUp(item.orderId)"
                  :style="{color: item.orderStatus === 0 ? '#67C23A' : '#E6A23C'}">
                  {{ item.orderStatus === 0 ? '下架' : '上架' }}
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页组件 -->
    <div class="pagination-wrapper">
      <Pagination
        @item-click="pageClick"
        :cUrl="url + ctype + '/'"
        :cTotal="total"
        :cPageSize="pageSize">
      </Pagination>
    </div>

    <!-- 编辑组件 -->
    <change-message :cupdateGoodInfo="updateGoodInfo"></change-message>
  </div>
</template>

<script>
import { selectOrderById, selectOrderByUsernameAndType, takeDown, takeUp } from "../api/order";
import ChangeMessage from "./ChangeMessage.vue";
import DeleteMessage from "./DeleteMessage.vue";
import Pagination from "./Pagination.vue";

export default {
  components: { ChangeMessage, DeleteMessage, Pagination },
  props: {
    ctype: {
      type: String,
      required: true
    },
    cdesciibe: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      userGoods: [],
      updateGoodInfo: {},
      searchValue: "",
      total: 0,
      pageSize: 30,
      url: "/order/search/"
    };
  },
  methods: {
    takeDown(orderId) {
      takeDown({ item: orderId })
        .then(() => {
          this.$message.success("下架成功");
          this.getData();
        })
        .catch(err => {
          console.error(err);
          this.$message.error("下架失败");
        });
    },
    takeUp(orderId) {
      takeUp({ item: orderId })
        .then(() => {
          this.$message.success("上架成功");
          this.getData();
        })
        .catch(err => {
          console.error(err);
          this.$message.error("上架失败");
        });
    },
    pageClick(items) {
      this.userGoods = items;
    },
    publishClick() {
      this.$router.push(`/home/addmessage/publish${this.ctype}`).catch(err => err);
    },
    changeInfo(orderId) {
      this.$store.commit("updateChangedOrderId", orderId);
      selectOrderById({
        order_id: this.$store.state.changedOrderId
      })
        .then(res => {
          this.updateGoodInfo = res.data;
        })
        .catch(err => {
          console.error("获取订单信息失败:", err);
          this.$message.error("获取订单信息失败");
        });
    },
    deleteInfo(orderId) {
      this.$store.commit("updateChangedOrderId", orderId);
    },
    handleSearch() {
      this.getData();
    },
    getData() {
      selectOrderByUsernameAndType({
        ctype: this.ctype,
        pageNum: this.pageNum,
        keys: this.searchValue
      })
        .then(res => {
          this.userGoods = res.data.list;
          this.total = res.data.total;
        })
        .catch(err => {
          console.error("获取订单列表失败:", err);
          this.$message.error("获取订单列表失败");
        });
    }
  },
  created() {
    this.getData();
  }
};
</script>

<style lang="less" scoped>
.published-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  
  .action-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    .publish-btn {
      height: 40px;
    }
    
    .search-box {
      width: 300px;
      
      /deep/ .el-input-group__append {
        background-color: #67C23A;
        color: white;
        border-color: #67C23A;
      }
    }
  }
  
  .goods-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
  }
  
  .goods-card {
    border: 1px solid #f2f2f2;
    border-radius: 6px;
    display: flex;
    padding: 10px;
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-3px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
    
    .goods-image {
      width: 200px;
      height: 180px;
      flex-shrink: 0;
      
      .goods-img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
      
      .image-error {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: #f5f7fa;
        color: #909399;
        font-size: 40px;
      }
    }
    
    .goods-info {
      flex: 1;
      padding-left: 10px;
      display: flex;
      flex-direction: column;
      
      .goods-title {
        margin: 0 0 10px;
        font-size: 16px;
        color: #303133;
        line-height: 1.4;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }
      
      .goods-meta {
        flex: 1;
        
        .goods-owner {
          font-size: 13px;
          color: #909399;
          
          i {
            margin-right: 5px;
          }
        }
        
        .goods-content {
          margin: 10px 0;
          color: #606266;
          font-size: 14px;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 3;
          overflow: hidden;
        }
      }
      
      .goods-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .goods-price {
          color: #F56C6C;
          font-weight: bold;
          font-size: 16px;
        }
        
        .goods-actions {
          .el-button {
            padding: 0;
            margin-left: 10px;
          }
        }
      }
    }
  }
  
  .pagination-wrapper {
    display: flex;
    justify-content: center;
    margin-top: 30px;
  }
}
</style>